<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka Producer 消息有序性与幂等性保证</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2px 8px;
            border-radius: 4px;
            display: inline-block;
            font-weight: 500;
        }
        
        .code-inline {
            background: #f3f4f6;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            font-size: 0.9em;
            color: #e11d48;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
            color: white;
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: scale(1.05);
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-stream mr-4"></i>
                Kafka Producer 核心机制
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解消息有序性与幂等性保证</p>
            <div class="flex justify-center space-x-8 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-sort-amount-down mr-2"></i>
                    <span>有序性保证</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>幂等性机制</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sync-alt mr-2"></i>
                    <span>事务支持</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12 animate-fade-in">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">K</span>afka Producer 的设计旨在支持高吞吐和分布式环境下的可靠消息投递。为了满足某些业务场景的严格要求，Kafka 提供了多个机制保证消息的 <span class="highlight-box">有序性</span> 和 <span class="highlight-box">幂等性</span>。这些机制在金融交易、订单处理等对数据一致性要求极高的场景中发挥着关键作用。
            </p>
        </div>

        <!-- 消息有序性保证 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12 animate-fade-in">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-sort-numeric-down mr-3 text-purple-600"></i>
                消息的有序性保证
            </h2>
            
            <p class="text-lg mb-8 text-gray-700 leading-relaxed">
                消息的有序性指的是消息按照产生的顺序被准确地发送、存储和消费。Kafka 的 Producer 使用以下机制确保消息有序：
            </p>

            <!-- 分区级别的有序性 -->
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6 mb-6">
                <h3 class="text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-layer-group mr-2 text-purple-600"></i>
                    1. 分区级别的有序性
                </h3>
                <div class="space-y-4">
                    <div>
                        <span class="font-semibold text-purple-700">原理：</span>
                        <span class="text-gray-700">Kafka 保证同一分区内的消息按照写入顺序存储，并且消费者按存储顺序消费。只要所有消息发往同一分区，就可以保证分区级别的顺序。</span>
                    </div>
                    <div>
                        <span class="font-semibold text-purple-700">实现方式：</span>
                        <ul class="mt-2 space-y-2 ml-6">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span class="text-gray-700">使用<strong>分区键（Partition Key）</strong>：通过为每条消息指定 <code class="code-inline">key</code>，Kafka 根据分区器的规则将同一 <code class="code-inline">key</code> 的消息路由到同一分区。</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span class="text-gray-700"><strong>默认分区器</strong>：对 <code class="code-inline">key</code> 进行哈希计算，然后将结果映射到分区。</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span class="text-gray-700"><strong>自定义分区器</strong>：实现自定义分区器以满足特定的业务需求。</span>
                            </li>
                        </ul>
                    </div>
                    <div class="mt-4 p-4 bg-yellow-50 rounded-lg">
                        <i class="fas fa-exclamation-triangle text-yellow-600 mr-2"></i>
                        <span class="text-gray-700"><strong>局限性：</strong>如果多个生产者并发发送消息到同一分区，可能导致消息乱序。要完全保证顺序，需要单一生产者发送消息。</span>
                    </div>
                </div>
            </div>

            <!-- 单生产者模型 -->
            <div class="bg-gradient-to-r from-blue-50 to-cyan-50 rounded-xl p-6 mb-6">
                <h3 class="text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-user mr-2 text-blue-600"></i>
                    2. 单生产者模型
                </h3>
                <p class="text-gray-700 leading-relaxed">
                    为了确保严格顺序，建议单个 Producer 实例向某分区发送消息。在这种情况下，Kafka 会按照 Producer 调用 <code class="code-inline">send</code> 方法的顺序将消息写入分区。这种模型在吞吐量需求较低或顺序性要求高的场景（如金融交易或订单处理）中适用。
                </p>
            </div>

            <!-- 消息重试与顺序性 -->
            <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl p-6 mb-6">
                <h3 class="text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-redo mr-2 text-green-600"></i>
                    3. 消息重试与顺序性
                </h3>
                <p class="text-gray-700 mb-4">
                    如果消息发送失败并启用了重试（<code class="code-inline">retries > 0</code>），重试机制可能会导致消息乱序。
                </p>
                <div class="p-4 bg-green-100 rounded-lg">
                    <span class="font-semibold text-green-700">解决方案：</span>
                    <span class="text-gray-700">开启<strong>幂等性</strong>和<strong>顺序重试</strong>，并将 <code class="code-inline">max.in.flight.requests.per.connection</code> 设置为 1。</span>
                </div>
            </div>

            <!-- 事务性 Producer -->
            <div class="bg-gradient-to-r from-indigo-50 to-purple-50 rounded-xl p-6">
                <h3 class="text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-exchange-alt mr-2 text-indigo-600"></i>
                    4. 事务性 Producer
                </h3>
                <p class="text-gray-700">
                    Kafka 提供事务支持（<code class="code-inline">enable.idempotence=true</code> 且启用事务 API），可以保证 Producer 在一个事务中发送的多条消息在逻辑上是有序的。事务支持还允许跨多个分区和主题的操作保持全局顺序。
                </p>
            </div>
        </div>

        <!-- 可视化图表 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12 animate-fade-in">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 text-center">
                <i class="fas fa-project-diagram mr-3 text-purple-600"></i>
                Kafka Producer 机制关系图
            </h3>
            <div class="mermaid">
                graph TD
                    A[Kafka Producer] --> B[消息有序性]
                    A --> C[消息幂等性]
                    
                    B --> B1[分区级别有序]
                    B --> B2[单生产者模型]
                    B --> B3[消息重试策略]
                    B --> B4[事务性Producer]
                    
                    C --> C1[幂等Producer模式]
                    C --> C2[PID和序列号]
                    C --> C3[重试机制协同]
                    
                    B4 --> D[事务与幂等性结合]
                    C1 --> D
                    
                    D --> E[强一致性保证]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:3px,color:#fff
            </div>
        </div>

        <!-- 消息幂等性保证 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12 animate-fade-in">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-shield-alt mr-3 text-purple-600"></i>
                消息的幂等性保证
            </h2>
            
            <p class="text-lg mb-8 text-gray-700 leading-relaxe